//@flow

/**
 * 登陆按钮
 */

import React, { Component } from 'react';
import { 
    View,
    Text,
    TouchableOpacity,
    ActivityIndicator,
 } from 'react-native';
import { 
    Toast,
 } from "antd-mobile-rn";
import * as api from "../../service/api";
import { AppVersionInfoModal } from '../../service/modal/responseModal/appVersionInfoModal';
import { ResponseResultStatus } from '../../service/business/ResponseResultStatus';
import { ResponseBaseModal } from '../../service/modal/baseModal/responseBaseModal';
import { realm } from '../../db/index';
import RNFS from "react-native-fs";

type Props = {
    username	:string,//登陆用：账号|手机号
    password    :string,//密码|验证码
    
}
/**按钮状态：
 * normal:各种合法，并且可以点击
 * disable：各种不合法无法登陆
 * loading：正在登陆状态
 */
type BtnStatus = 'normal'|'disable'|'loading'


type State = {
    status  :BtnStatus,//按钮状态
 
}
const HEIHGT : number = 50;
export class LoginBtn extends Component<Props,State>{
    state :State = {
        status : 'disable'
    }
    async onClick(){
        realm.write(()=>{
            console.log(`数据库地址:${RNFS.DocumentDirectoryPath}`)
            let res:Realm.Results = realm.objects('LaunchRecordTB');
            console.log(`查询结果:${res},条数：${res.length}`)
            if(realm){
                try {
                    realm.create('LaunchRecordTB',{version:'1.0.0',updateTime:new Date(),uid:"123",times:3,account:'123',pwd:"123",token:"123"},true)    
                } catch (error) {
                    console.log(`创建数据库错误:${error}`)
                }
                
            }else{
                console.log(`realm为空： ${realm}`);
            }
            // let count : number = 1;
            // if(res){
            //     count = res.times + 1;
            // }
            // realm.create('LaunchRecordTB',{version:'1.0.1',updateTime:new Date(),uid:'123',times:count},true)
            // console.log(`查询结果:${res},次数：${res.times}`)
        })
    }
    render(){
        return(
            <TouchableOpacity
                onPress={this.onClick}
                style={{
                    borderRadius:HEIHGT / 2,
                    height:HEIHGT,
                    alignItems:'center',
                    justifyContent:'center',
                    backgroundColor: 'white',
                    marginTop:30,
                    width:'100%',
            }}>
                <View>
                    {
                        this.state.status == 'loading' ? <ActivityIndicator animating={true}/>
                                                       : null
                    }
                    <Text style={{color:'blue'}}>登录</Text>
                </View>
            </TouchableOpacity>
        )
    }
}